/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package do_.com.bpd.hrtoolspersistence.configuration;

import do_.com.bpd.hrtoolspersistence.configuration.entities.HrtoolsConfig;
import do_.com.bpd.hrtoolspersistence.controllers.HrtoolsConfigJpaController;
import do_.com.bpd.hrtoolspersistence.entities.exceptions.NonexistentEntityException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Developer
 */
public class ConfigManager {

    public void setConfig(String configName, String configValue) throws Exception {
        setConfig(configName, configValue, "");
    }

    public void setConfig(String configName, String configValue, String comment) throws Exception {

        if (configName.length() > 255) {
            throw new Exception("Config name cannot exceed 255 characters");
        }

        HrtoolsConfigJpaController cont = null;
        EntityManager em = null;
        try {
            cont = new HrtoolsConfigJpaController();
            em = cont.getEntityManager();

            Query q = em.createNamedQuery("HrtoolsConfig.findByConfigName");
            q.setParameter("configName", configName);
            List<HrtoolsConfig> resultList = q.getResultList();
            HrtoolsConfig hc = null;
            if (resultList.size() > 0) {
                hc = resultList.get(0);
                hc.setConfigValue(configValue);
                hc.setComment(comment);
                try {
                    cont.edit(hc);
                } catch (NonexistentEntityException ex) {
                    Logger.getLogger(ConfigManager.class.getName()).log(Level.SEVERE, null, ex);
                    throw new Exception(ex.getMessage());
                } catch (Exception ex) {
                    Logger.getLogger(ConfigManager.class.getName()).log(Level.SEVERE, null, ex);
                    throw new Exception(ex.getMessage());
                }
            } else {
                hc = new HrtoolsConfig();
                hc.setConfigName(configName);
                hc.setConfigValue(configValue);
                hc.setComment(comment);
                cont.create(hc);
            }
        } finally {
            if (em != null) {
                em.close();
            }
            if (cont != null) {
                cont.closeEntityManagerFactory();
            }
        }
    }

    public String getConfig(String configName) throws Exception {
        return getConfig(configName, "");
    }

    public String getConfig(String configName, String defaultValue) throws Exception {
        return getConfig(configName, defaultValue, null);
    }

    public String getConfig(String configName, String defaultValue, String comment) throws Exception {

        HrtoolsConfigJpaController cont = null;
        EntityManager em = null;
        try {
            cont = new HrtoolsConfigJpaController();
            em = cont.getEntityManager();

            Query q = em.createNamedQuery("HrtoolsConfig.findByConfigName");
            q.setParameter("configName", configName);
            List<HrtoolsConfig> resultList = q.getResultList();
            HrtoolsConfig hc = null;
            if (resultList.size() > 0) {
                hc = resultList.get(0);
                return hc.getConfigValue();
            } else {
                if (null != comment) {
                    setConfig(configName, defaultValue, comment);
                } else {
                    setConfig(configName, defaultValue);
                }
                return getConfig(configName);
            }
        } finally {
            if (em != null) {
                em.close();
            }
            if (cont != null) {
                cont.closeEntityManagerFactory();
            }
        }
    }
}
